﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace FreeFlow.Administration
{
  /// <summary>
  /// Details of an instance of a Windows Workflow
  /// </summary>
  public class WorkflowInstance : DatabaseAware, IDeletable, IRefreshable
  {
    internal WorkflowInstance(Server server, IDataReader reader) : base(server)
    {
      guid = new Guid(reader["eWorkflowInstance"].ToString());
      parentFolderId = reader["eFolderID"].ToString();
      created = (DateTime)reader["CreatedTime"];
    }

    private Guid guid;
    /// <summary>
    /// Gets the GUID that uniquely identifies the workflow instance.
    /// </summary>
    public Guid Guid
    {
      get { return guid; }
    }

    private string parentFolderId;
    /// <summary>
    /// Gets the folder ID of the folder that created this workflow instance.
    /// </summary>
    public string ParentFolderId
    {
      get
      {
        return parentFolderId;
      }
    }

    private Folder parent;
    /// <summary>
    /// Gets the parent folder for this folder.
    /// </summary>
    public Folder GetParent()
    {
      if (parent == null)
        parent = Server.GetFolder(parentFolderId);
      return parent;
    }

    private DateTime created;
    /// <summary>
    /// Gets the date and time when the workflow instance was created.
    /// </summary>
    public DateTime Created
    {
      get
      {
        return created;
      }
    }

    private WorkflowInstanceTrackingCollection trackingData;
    /// <summary>
    /// Gets the tracking data for this workflow instance.
    /// </summary>
    public WorkflowInstanceTrackingCollection TrackingData
    {
      get
      {
        if (trackingData == null)
        {
          trackingData = new WorkflowInstanceTrackingCollection(Server, guid);
        }
        return trackingData;
      }
    }

    #region IDeletable Members

    /// <summary>
    /// Deletes this workflow instance from the database.
    /// </summary>
    public void Delete()
    {
      using (IDbTransaction transaction = Server.BeginTransaction())
      {
        Server.ExecuteNonQuery(transaction,
          string.Format("DELETE FROM eMSWorkflowEvent WHERE eWorkflowInstance ='{0}'",
          guid));
        Server.ExecuteNonQuery(transaction,
          string.Format("DELETE FROM eMSWorkflowTracking WHERE eWorkflowInstance ='{0}'",
          guid));
        Server.ExecuteNonQuery(transaction,
          string.Format("DELETE FROM CompletedScope WHERE uidInstanceID ='{0}'",
          guid));
        Server.ExecuteNonQuery(transaction,
          string.Format("DELETE FROM InstanceState WHERE uidInstanceID ='{0}'",
          guid));
        transaction.Commit();
      }
    }

    #endregion

    #region IRefreshable Members

    /// <summary>
    /// Refreshes the data for the workflow instance.
    /// </summary>
    public void Refresh()
    {
      trackingData = null;
    }

    #endregion
  }
}
